home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / WASTE Object Handlers v1.0 ƒ / Handler Source / WE_snd_Handler.c < prev   
Encoding:
Text File  |  1995-03-16  |  4.0 KB  |  198 lines  |  [TEXT/MPCC]

  1. // Sound Object Handler for the WASTE Text Engine
  2. // by Michael F. Kamprath, kamprath@earthlink.net
  3. //
  4. // v1.0, 16 March 1995
  5. //
  6.  
  7. #include <Icons.h>
  8. #include <SoundInput.h>
  9. #include "WASTE.h"
  10.  
  11. #include "WE_snd_Handler.h"
  12. #include "WASTE_Objects.h"
  13.  
  14. //
  15. // Sound Channel and current playing sound pointers
  16. //
  17. SndChannelPtr    gSoundChannel = nil;
  18. SndListHandle    gCurrentSound = nil;
  19.  
  20. //
  21. // New Object Handler for sounds
  22. //
  23. pascal OSErr    HandleNewSound(Point *defaultObjectSize,WEObjectReference objectRef)
  24. {
  25.     (*defaultObjectSize).h = 32;
  26.     (*defaultObjectSize).v = 32;
  27.  
  28.     return(noErr);
  29. }
  30. //
  31. // Dispose Object handler for sounds
  32. //
  33.  
  34. pascal OSErr    HandleDisposeSound(WEObjectReference objectRef )
  35. {
  36. Handle    theSound;
  37.  
  38.     theSound = WEGetObjectDataHandle(objectRef);
  39.  
  40.     DisposHandle(theSound);
  41.  
  42.     return(MemError());
  43. }
  44. //
  45. // Draw Object handler for sounds
  46. //
  47. pascal OSErr    HandleDrawSound(Rect *destRect, WEObjectReference objectRef )
  48. {
  49. OSErr    iErr;
  50.     
  51.     iErr = PlotIconID(destRect,atNone,ttNone,kSoundIconID);
  52.     
  53.     return( iErr );
  54. }
  55. //
  56. // Click Object Handler for sounds
  57. //
  58. static long    oldSoundClickTime = nil;
  59.  
  60. pascal OSErr    HandleClickSound(    Point hitPt, 
  61.                             short modifiers, 
  62.                             long clickTime, 
  63.                             WEObjectReference objectRef)
  64. {
  65. SndListHandle    theSound = (SndListHandle)WEGetObjectDataHandle( objectRef );
  66. OSErr            iErr = noErr;
  67.  
  68.  
  69.     if (oldSoundClickTime + GetDblTime() > clickTime )
  70.     {
  71.         iErr = PlaySoundHandle( theSound );
  72.     }
  73.     
  74.     oldSoundClickTime = clickTime;
  75.             
  76.     return( iErr );
  77. }
  78.  
  79. //
  80. // PlaySelectedSound()
  81. //        If a sound object, and only a sound object, is selected,
  82. //        PlaySelectedSound() will play it.
  83. //
  84. OSErr    PlaySelectedSound( WEHandle theWE )
  85. {
  86. WEObjectReference    objectRef;
  87. SndListHandle        theSound;
  88. OSErr                iErr = noErr;
  89.  
  90.     iErr = WEGetSelectedObject( &objectRef, theWE );
  91.     if (!iErr)
  92.     {
  93.         theSound = (SndListHandle)WEGetObjectDataHandle( objectRef );
  94.         iErr = PlaySoundHandle( theSound );
  95.     }
  96.         
  97.     return( iErr );
  98. }
  99. //
  100. // CreateNewSoundObject()
  101. //        Uses built in sound recording to create a new sound object.
  102. //
  103. OSErr    CreateNewSoundObject( WEHandle theWE )
  104. {
  105. OSErr            iErr;
  106. SndListHandle    sndHandle = nil;
  107. Point            corner = {32,32};
  108.  
  109.     iErr = SndRecord(nil,corner,siGoodQuality,&sndHandle);
  110.     
  111.     if (iErr == noErr)
  112.     {
  113.         corner.h = 32;
  114.         corner.v = 32;
  115.         WEInsertObject( 'snd ', (Handle)sndHandle, corner, theWE );
  116.     }
  117.     
  118.     return( noErr );
  119. }
  120.  
  121. //
  122. // PlaySoundHandle()
  123. //        Ultility routine to play a sound async.  Maintains a sound channel 
  124. //        and the sound data to do so.
  125. //
  126.  
  127. OSErr    PlaySoundHandle( SndListHandle theSound )
  128. {
  129. OSErr        iErr = noErr;
  130. SndCommand    cmd;
  131.  
  132.     // check for sound channel
  133.     if (!gSoundChannel)
  134.         gSoundChannel = CreateNewSoundChannel();
  135.     
  136.     // If a sound is currently playing, stop it and dispose of it's buffer.
  137.     if (gCurrentSound)
  138.     {
  139.         cmd.cmd = quietCmd;
  140.         iErr = SndDoImmediate(gSoundChannel,&cmd);
  141.         HUnlock( (Handle)gCurrentSound );
  142.         DisposHandle( (Handle)gCurrentSound );
  143.         gCurrentSound = nil;
  144.     }
  145.     
  146.     // create a new buffer for the selected sound.
  147.     gCurrentSound = theSound;
  148.     iErr = HandToHand((Handle *)&(gCurrentSound));
  149.     
  150.     if (!iErr)
  151.     {
  152.         // Lock the buffer and play the sound.
  153.         HLockHi( (Handle)gCurrentSound );
  154.         iErr = SndPlay(gSoundChannel,gCurrentSound,true);
  155.     }
  156.     
  157.     return(iErr);
  158. }
  159.  
  160. //
  161. // CreateNewSoundChannel()
  162. //        Used to create a new shound channel.
  163. //
  164. SndChannelPtr    CreateNewSoundChannel( void )
  165. {
  166. OSErr            iErr;
  167. SndChannelPtr    chan;
  168.         
  169.     // allocate a sound channel
  170.     chan = (SndChannelPtr)NewPtrClear(sizeof(SndChannel));
  171.     if ( chan != nil ) 
  172.     {
  173.         chan->qLength = stdQLength;     //128 sound commands
  174.         iErr = SndNewChannel(&chan, sampledSynth, initMono, nil);
  175.     }
  176.     return(chan);            // return SndChannelPtr
  177. }
  178.  
  179. //
  180. // CheckSoundStatus()
  181. //        Should be called periodically to dispose of unneeded sound buffers.
  182. //
  183. void CheckSoundStatus( void )
  184. {
  185. OSErr        iErr;
  186. SCStatus    theStatus;
  187.  
  188.     if (gSoundChannel)
  189.     {
  190.         iErr = SndChannelStatus(gSoundChannel,sizeof(SCStatus),&theStatus);
  191.         if (( !theStatus.scChannelBusy )&&(gCurrentSound))
  192.         {
  193.             HUnlock( (Handle)gCurrentSound );
  194.             DisposHandle( (Handle)gCurrentSound );
  195.             gCurrentSound = nil;
  196.         }    
  197.     }        
  198. }